//
//  XLAlert.m
//  campus
//
//  Created by xl on 2019/12/15.
//  Copyright © 2019 xl. All rights reserved.
//

#import "XLAlert.h"

@implementation XLAlert

+ (void)showAlert:(NSString *)title
{
    [self showAlertTitle:title message:nil];
}

+ (void)showAlertTitle:(NSString *)title message:(NSString *)message
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:okAction];
    UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    
    [topRootViewController presentViewController:alertController animated:YES completion:nil];
}

+ (void)showAlertTitle:(NSString *)title okTitle:(NSString *)okTitle {
    [self showAlert:title message:nil cancelTitle:nil okTitle:okTitle cancelHandler:nil okHandler:nil];

}

+ (void)showAlertTitle:(NSString *)title okTitle:(NSString *)okTitle okHandler:(void(^)(UIAlertAction *action))handlerk {
    [self showAlert:title message:nil cancelTitle:nil okTitle:okTitle cancelHandler:nil okHandler:handlerk];
}

+ (void)showAlertTitle:(NSString *)title
               message:(NSString *)message
               okTitle:(NSString *)okTitle
      messageAlignment:(NSTextAlignment)messageAlignment
             okHandler:(void(^)(UIAlertAction *action))handlerk {
    [self showAlert:title message:message messageAlignment:messageAlignment cancelTitle:nil okTitle:okTitle cancelHandler:nil okHandler:handlerk];
}

+ (void)showAlert:(NSString *)title okHandler:(void(^)(UIAlertAction *action))handlerk
{
    [self showAlert:title cancelTitle:@"取消" okTitle:@"好的" okHandler:handlerk];
}

+ (void)showAlert:(NSString *)title cancelTitle:(NSString *)cancelTitle okTitle:(NSString *)okTitle okHandler:(void(^)(UIAlertAction *action))handlerk
{
    [self showAlert:title message:nil cancelTitle:cancelTitle okTitle:okTitle okHandler:handlerk];
}

+ (void)showAlert:(NSString *)title cancelTitle:(NSString *)cancelTitle cancelHandler:(void(^)(UIAlertAction *action))handlerk
{
    [self showAlert:title message:nil cancelTitle:cancelTitle okTitle:nil cancelHandler:nil okHandler:handlerk];
}

+ (void)showAlert:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle okTitle:(NSString *)okTitle okHandler:(void(^)(UIAlertAction *action))handlerk
{
    [self showAlert:title message:message cancelTitle:cancelTitle okTitle:okTitle cancelHandler:nil okHandler:handlerk];
}

+ (void)showAlert:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle okTitle:(NSString *)okTitle cancelHandler:(void(^)(UIAlertAction *action))cancelHander okHandler:(void(^)(UIAlertAction *action))handlerk
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if (!ISEmptyString(cancelTitle)) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:cancelHander];
        [alertController addAction:cancelAction];
    }
    
    if (!ISEmptyString(okTitle)) {
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:handlerk];
        [alertController addAction:okAction];
    }
    
    UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    
    [topRootViewController presentViewController:alertController animated:YES completion:nil];
    
}

+ (void)showAlert:(NSString *)title message:(NSString *)message messageAlignment:(NSTextAlignment)messageAlignment cancelTitle:(NSString *)cancelTitle okTitle:(NSString *)okTitle cancelHandler:(void(^)(UIAlertAction *action))cancelHander okHandler:(void(^)(UIAlertAction *action))handlerk
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if (!ISEmptyString(cancelTitle)) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:cancelHander];
        [alertController addAction:cancelAction];
    }
    
    if (!ISEmptyString(okTitle)) {
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:handlerk];
        [alertController addAction:okAction];
    }
    if (!ISEmptyString(message)) {
        UIView *subView1 = alertController.view.subviews[0];
        UIView *subView2 = subView1.subviews[0];
        UIView *subView3 = subView2.subviews[0];
        UIView *subView4 = subView3.subviews[0];
        UIView *subView5 = subView4.subviews[0];
        if (subView5.subviews.count > 2 && [subView5.subviews[2] isKindOfClass:[UILabel class]]) {
            XLLabel *messageLabel = subView5.subviews[2];
            messageLabel.textAlignment = messageAlignment;
        }
    }
    UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    
    [topRootViewController presentViewController:alertController animated:YES completion:nil];
    
}

+ (void)showAlert:(NSString *)title okHandler:(void(^)(UIAlertAction *action))handlerk cancelHandler:(void(^)(UIAlertAction *action))cancelHander
{
    [self showAlert:title okTitle:@"确认" okHandler:handlerk cancelHandler:cancelHander];
}

+ (void)showAlert:(NSString *)title okTitle:(NSString *)okTitle okHandler:(void(^)(UIAlertAction *action))handlerk cancelHandler:(void(^)(UIAlertAction *action))cancelHander
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:handlerk];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:cancelHander];
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }

    [topRootViewController presentViewController:alertController animated:YES completion:nil];
}

+ (void)showTextFieldAlert:(NSString *)title placeholder:(NSString *)placeholder okHandler:(void(^)(UIAlertAction *action))handlerk onfigurationHandler:(void (^)(UITextField *textField))configurationHandler
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:handlerk];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = placeholder;
        textField.secureTextEntry = YES;
        configurationHandler(textField);
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    
    [topRootViewController presentViewController:alertController animated:YES completion:nil];
}

+ (void)showInputTextFieldAlert:(NSString *)title
                        message:(NSString *)message
                      okHandler:(void(^)(NSString *content))content
            onfigurationHandler:(void (^)(UITextField *  textField))configurationHandler {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        content(alertController.textFields.firstObject.text);
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        configurationHandler(textField);
    }];
    [alertController addAction:okAction];
    UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    
    [topRootViewController presentViewController:alertController animated:YES completion:nil];
}

+ (void)showUpdatingAlert:(NSDictionary *)dic
{
    //需要提示更新
    NSInteger type = [[dic objectForKey:@"type"] integerValue];
    if (type !=1 && type != 2) {
        return;
    }
    NSString *content = [dic objectForKey:@"content"];
    NSString *versionNo = [dic objectForKey:@"versionNo"];
    NSMutableAttributedString *message = [[NSMutableAttributedString alloc] initWithString:content];
    [message addAttribute:NSForegroundColorAttributeName value:[UIColor grayFontColor] range:NSMakeRange(0, message.length)];
    [message addAttribute:NSFontAttributeName value:[UIFont fontOfPingFangSCRegularWithSize:13] range:NSMakeRange(0, message.length)];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"发现新版本V%@",versionNo] message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController setValue:message forKey:@"attributedMessage"];
    
    if (type == 1) {
        //强制更新
    }
    else if (type == 2)
    {
        //普通更新
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:cancelAction];
    }
    
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"立即升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *appUrl = [dic objectForKey:@"url"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appUrl]];
        if (type == 1) {
            [XLAlert showUpdatingAlert:dic];
        }
    }];
    [alertController addAction:okAction];
    UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    
    [topRootViewController presentViewController:alertController animated:YES completion:nil];
    
}
@end
